package com.example.ice.fittime.util;

import android.content.Context;
import android.util.Log;

import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 */

public class DaoUtils {
    private DaoManage manager;

    public DaoUtils(Context context) {
        manager = DaoManage.getInstance();
        manager.init(context);
    }


    public <T> boolean insert(T entity) {
        boolean flag = false;

        flag = manager.getDaoSession().insert(entity) != -1 ? true : false;
        Log.i("CommonUtils", "----insertStudent--result is -->>" + flag);
        return flag;
    }

    public <T> boolean insertMult(final List<T> entity) {
        boolean flag = false;
        try {
            manager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (T entitys : entity) {
                        manager.getDaoSession().insertOrReplace(entitys);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public <T> boolean update(T entity) {
        boolean flag = false;
        try {
            manager.getDaoSession().update(entity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    public <T> boolean delete(T entity) {
        boolean flag = false;
        try {
            //按照指定的id进行删除 delete from student where _id = ?
            manager.getDaoSession().delete(entity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    public void deleteAll(Class cls){
        manager.getDaoSession().deleteAll(cls);
    }

    public <T> List<T> listAll(Class<T> entity) {
        return (List<T>) manager.getDaoSession().loadAll(entity);
    }


    public <T> T listOne(Class<T> entity, long key) {
        return manager.getDaoSession().load(entity, key);
    }

    public <T> void query1(Class<T> entity,String where, String... selectionArgs) {
        //使用native sql进行查询操作，
        List<T> list = manager.getDaoSession().queryRaw(entity, where, selectionArgs);
        Log.i("--->>", "" + list);
    }

//    /**
//     * select  * from student where name like ? or name =? or
//     * <  <= >= !=  in between and
//     * select * from student where age > 23 and address like "江西"
//     */
//    public <T> void query2(Class<T> entity) {
//        //查询构建器
//        QueryBuilder<T> builder = manager.getDaoSession().queryBuilder(entity);
//        List<T> list = builder.where(StudentDao.Properties.Age.ge(23)).where(StudentDao.Properties.Address.like("江西")).list();
//        Log.i("--->>", "" + list);
//    }
//
//    public void query3() {
//
//        //逻辑与 和 逻辑或 是双目运算符
//        QueryBuilder<Student> builder = manager.getDaoSession().queryBuilder(Student.class);
//        //select * from student where (address='北京' or age > 50) and name like '%张%'
//        builder.whereOr(StudentDao.Properties.Address.eq("北京"), StudentDao.Properties.Age.ge(50));
//        builder.whereOr(StudentDao.Properties.Id.ge(2), StudentDao.Properties.Age.ge(10)).limit(3);//区前三条数据
//
//        //builder.where(StudentDao.Properties.Name.like("张"));
//        List<Student> list = builder.list();
//        Log.i("--->>", "" + list);
//    }
}
